//
//  TestViewController.m
//  aTest
//
//  Created by 黄杰 on 15/10/30.
//  Copyright © 2015年 黄杰. All rights reserved.
//

#import "TestViewController.h"
#import "TestViewModel.h"

@interface TestViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dateArray;
@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"框架测试";
    
    [self setupData];
    
    self.tabBarItem.badgeValue = @"10";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dateArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dddd"];
    
    // 2.如果没有可以利用的cell。
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dddd"];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [self.dateArray[indexPath.row] title];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    TestModel *model = self.dateArray[indexPath.row];
    
    Class className = NSClassFromString(model.className);
    [self.navigationController pushViewController:[[className alloc] init] animated:YES];
}

/**
 *  加载数据
 */
- (void)setupData
{
    __weak typeof(self) weakSelf = self;
    [TestViewModel setupDataWithComplete:^(NSMutableArray *dictArray) {
        weakSelf.dateArray = dictArray;
        [weakSelf.tableView reloadData];
    }];

}

@end
